<?php

class Constant_expense extends CActiveRecord
{
	/**
	 * The followings are the available columns in table 'Constant_expense':
	 * @var integer $id
	 * @var string $plate_no
	 * @var double $depreciation
	 * @var double $handling_cost
	 * @var double $wage_expenditure
	 * @var double $gps_cost
	 * @var double $another_cost
	 * @var double $the_other_cost
	 * @var integer $state
	 * @var string $create_time
	 */
	public $plate_no;
	
	/**
	 * Returns the static model of the specified AR class.
	 * @return CActiveRecord the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'constant_expense';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		return array(
			array('plate_no','length','max'=>10),
			array('plate_no,depreciation,handling_cost,wage_expenditure,gps_cost,effect_date,lapse_date', 'required'),
			array('depreciation, handling_cost, wage_expenditure, gps_cost, another_cost, the_other_cost', 'numerical'),
			array('depreciation, handling_cost, wage_expenditure, gps_cost, another_cost, the_other_cost', 
			'match' ,'pattern' => '/^\d{1,16}(\.\d{1,2})?$/','message'=>'{attribute} 整数位最多16位，小数位2位'),
			array('effect_date,lapse_date', 'match','pattern' => '/\d{4}-\d{2}$/','message'=>'{attribute} 必须为日期型(年-月)'),
		);
	}
	
	/**
	 * Prepares attributes before performing validation.
	 */
	protected function beforeValidate($on)//在验证之前将无需用户输入的字段写入
	{
		if($this->isNewRecord){
			$this->state=0;
			$this->create_time=date('Y-m-d G:i:s');
		}
		if(trim($this->another_cost)=='') $this->another_cost=0;
		if(trim($this->the_other_cost)=='') $this->the_other_cost=0;
		return true;
	}
	
	protected function afterValidate($on)//在验证之前将无需用户输入的字段写入
	{	
		if($this->hasErrors()) return;
		$this->effect_date.='-01';
		$this->lapse_date.='-01';
		return true;
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => '索引号',
			'plate_no' => '车牌号',
			'fk_truck_id' => '车辆id',
			'depreciation' => '折旧费(元)',
			'handling_cost' => '管理成本(元)',
			'wage_expenditure' => '工资性支出(元)',
			'gps_cost' => 'GPS服务费(元)',
			'another_cost' => '其他费用1(元)',
			'the_other_cost' => '其他费用2(元)',
			'effect_date' => '生效日期',
			'lapse_date' => '失效日期',
			'state' => '状态',
			'create_time' => '创建时间',
		);
	}
	
	public function safeAttributes(){
		$params=parent::safeAttributes();
		$params[]='plate_no';
		return $params;
	}
}